﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Globalization;



public partial class Cost_UserControl_uc_XuatReport : System.Web.UI.UserControl
{
    public int _MasterID;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // Lay datatable phi
    private DataTable ConvertToDataTable<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult>(List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    // Lay master
    private DataTable ConvertToDataTableMaster<ZD_PRC_RPT_ZD_COST_MASTERResult>(List<ZD_PRC_RPT_ZD_COST_MASTERResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_RPT_ZD_COST_MASTERResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_RPT_ZD_COST_MASTERResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    // Lay detail
    private DataTable ConvertToDataTableDetail<ZD_PRC_RPT_ZD_COST_DETAILResult>(List<ZD_PRC_RPT_ZD_COST_DETAILResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_RPT_ZD_COST_DETAILResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_RPT_ZD_COST_DETAILResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    // Lay detailGroup
    private DataTable ConvertToDataTableDetailGroup<ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult>(List<ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult> data)
    {
        PropertyDescriptorCollection properties =
           TypeDescriptor.GetProperties(typeof(ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult));
        DataTable table = new DataTable();
        foreach (PropertyDescriptor prop in properties)
            table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
        foreach (ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult item in data)
        {
            DataRow row = table.NewRow();
            foreach (PropertyDescriptor prop in properties)
                row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
            table.Rows.Add(row);
        }
        return table;

    }

    protected void btnXuat_Click(object sender, EventArgs e)
    {
        OutReport outRPT = new OutReport();
        //lay database
        CostBO pur = new CostBO();
        //lay phi group 1
        List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult> rscost1 = new List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult>();
        rscost1 = pur.rpt_getPhiByGroupId(_MasterID, 1);
        DataTable dtcost1 = ConvertToDataTableMaster(rscost1);

        //lay phi group 2
        List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult> rscost2= new List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult>();
        rscost2 = pur.rpt_getPhiByGroupId(_MasterID, 2);
        DataTable dtcost2 = ConvertToDataTableMaster(rscost2);

        //lay phi group 3
        List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult> rscost3 = new List<ZD_PRC_ZD_COST_DSCHIPHI_GETPHIResult>();
        rscost3 = pur.rpt_getPhiByGroupId(_MasterID,3);
        DataTable dtcost3 = ConvertToDataTableMaster(rscost3);
        
        //master
        List<ZD_PRC_RPT_ZD_COST_MASTERResult> rsmaster = new List<ZD_PRC_RPT_ZD_COST_MASTERResult>();
        rsmaster = pur.rpt_getMasterByMasterId(_MasterID);
        DataTable dtmaster = ConvertToDataTableMaster(rsmaster);
        
        //detail group
        List<ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult> rsdetailGroup = new List<ZD_PRC_RPT_ZD_COST_DETAIL_GROUPResult>();
        rsdetailGroup = pur.rpt_getDetailGroupByMasterId(_MasterID);
        DataTable dtdetailGroup = ConvertToDataTableDetailGroup(rsdetailGroup);
        //detail
        List<ZD_PRC_RPT_ZD_COST_DETAILResult> rsdetail = new List<ZD_PRC_RPT_ZD_COST_DETAILResult>();
        rsdetail = pur.rpt_getDetailByMasterId(_MasterID);
        DataTable dtdetail = ConvertToDataTableDetail(rsdetail);
        
        // datatable cost
        DataTable ReportCost1 = new DataTable();
        DataTable ReportCost2 = new DataTable();
        DataTable ReportCost3 = new DataTable();
        ReportCost1 = dtcost1;
        ReportCost2 = dtcost2;
        ReportCost3 = dtcost3;
        ReportCost1.TableName = "PHI1";
        ReportCost2.TableName = "PHI2";
        ReportCost3.TableName = "PHI3";
        // datatable master
        DataTable ReportMaster = new DataTable();
        ReportMaster = dtmaster;
        ReportMaster.TableName = "MASTER";
        // datatable detailgroup
        DataTable ReportDetailGroup = new DataTable();
        ReportDetailGroup = dtdetailGroup;
        ReportDetailGroup.TableName = "ReportGroup";
        // datatable detail
        DataTable ReportDetail = new DataTable();
        ReportDetail = dtdetail;
        ReportDetail.TableName = "Report";

        DataTable[] arrTable = { ReportCost1, ReportCost2, ReportCost3, ReportMaster, ReportDetail, ReportDetailGroup };
        outRPT.OutReportToScreen(MapPath("~/RPTTemplate/LANDEDCOST_TEMPLATE.xls"), arrTable, "_LandedCost.xls");
    }
}
